# 预祝数据文件

from flask import Blueprint
from flask.cli import with_appcontext
from configs import *

from modules.Tables import *  # 导入的模型

import json

cli = Blueprint('cli', __name__)

# 模型类型数据预置
@cli.cli.command('insert_predefined_data')
@with_appcontext
def insert_predefined_data_command():
    if not Menu.query.first():

        meuu_data = [

            {'id':1,'menu_name':'实时视频','menu_link':'sssp','menu_icon':'el-icon-video-camera','menu_order':1,'menu_parent_id':None,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':2,'menu_name':'报警信息','menu_link':None,'menu_icon':'el-icon-warning-outline','menu_order':2,'menu_parent_id':None,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':3,'menu_name':'报警列表','menu_link':'bjlb','menu_icon':'el-icon-warning-outline','menu_order':3,'menu_parent_id':2,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':4,'menu_name':'报警统计','menu_link':'bjtj','menu_icon':'el-icon-warning-outline','menu_order':4,'menu_parent_id':2,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':5,'menu_name':'算法仓','menu_link':None,'menu_icon':'el-icon-monitor','menu_order':5,'menu_parent_id':None,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':6,'menu_name':'算法配置','menu_link':'sfgl','menu_icon':'el-icon-monitor','menu_order':6,'menu_parent_id':5,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':7,'menu_name':'算法管理','menu_link':'sfc','menu_icon':'el-icon-monitor','menu_order':7,'menu_parent_id':5,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':8,'menu_name':'基础配置','menu_link':None,'menu_icon':'el-icon-full-screen','menu_order':8,'menu_parent_id':None,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':9,'menu_name':'设备添加','menu_link':'sbtj','menu_icon':'el-icon-full-screen','menu_order':9,'menu_parent_id':8,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':10,'menu_name':'录像机设备同步','menu_link':'sbtb','menu_icon':'el-icon-full-screen','menu_order':10,'menu_parent_id':8,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':11,'menu_name':'视频质量诊断','menu_link':None,'menu_icon':'el-icon-cpu','menu_order':11,'menu_parent_id':None,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':12,'menu_name':'预案配置','menu_link':'spzlzdpz','menu_icon':'el-icon-cpu','menu_order':12,'menu_parent_id':11,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},
            {'id':13,'menu_name':'诊断结果','menu_link':'spzlzd','menu_icon':'el-icon-cpu','menu_order':13,'menu_parent_id':11,
             'menu_permission_list':json.dumps([1,2]),'menu_status':'1'},

            {'id': 15, 'menu_name': '系统管理', 'menu_link': None, 'menu_icon': None, 'menu_order': 14,
             'menu_parent_id': None,
             'menu_permission_list': json.dumps([1, 2]), 'menu_status': '1'},
            {'id': 16, 'menu_name': '用户管理', 'menu_link': 'user', 'menu_icon': None, 'menu_order': 15,
             'menu_parent_id': 15,
             'menu_permission_list': json.dumps([1, 2]), 'menu_status': '1'},
            {'id': 17, 'menu_name': '角色管理', 'menu_link': 'role', 'menu_icon': None, 'menu_order': 16,
             'menu_parent_id': 15,
             'menu_permission_list': json.dumps([1, 2]), 'menu_status': '1'},
            {'id': 18, 'menu_name': '字典管理', 'menu_link': 'dict', 'menu_icon': None, 'menu_order': 17,
             'menu_parent_id': 15,
             'menu_permission_list': json.dumps([1, 2]), 'menu_status': '1'},
            {'id': 19, 'menu_name': '菜单管理', 'menu_link': 'menu', 'menu_icon': None, 'menu_order': 18,
             'menu_parent_id': 15,
             'menu_permission_list': json.dumps([1, 2]), 'menu_status': '1'},
            {'id': 20, 'menu_name': '视频分析', 'menu_link': 'spfx', 'menu_icon': None, 'menu_order': 19,
             'menu_parent_id': None,
             'menu_permission_list': json.dumps([1, 2]), 'menu_status': '1'},
            {'id': 14, 'menu_name': '云管中心', 'menu_link': 'ygzx', 'menu_icon': 'el-icon-video-camera', 'menu_order': 20,
             'menu_parent_id': None, 'menu_permission_list': json.dumps([1, 2]), 'menu_status': '1'},



        ]

        for i in meuu_data:
            menu_res = Menu(id=i['id'],menu_name=i['menu_name'],menu_link=i['menu_link'],menu_order=i['menu_order'],menu_icon=i['menu_icon'],
                            menu_parent_id=i['menu_parent_id'],menu_permission_list=i['menu_permission_list'],menu_status=i['menu_status'])
            db.session.add(menu_res)
        db.session.commit()
        print('菜单预置数据插入完成！')

    if not Permission.query.first():

        permission_data = [
            {'id':1,'permission_name':'超级管理员','permission_code':None,'permission_content':'增删改查','permission_status':'1'},
            {'id':2,'permission_name':'普通用户','permission_code':None,'permission_content':'增删改查','permission_status':'1'},
        ]

        for i in permission_data:
            permission_res = Permission(id=i['id'],permission_name=i['permission_name'],permission_code=i['permission_code'],
                                        permission_content=i['permission_content'],permission_status=i['permission_status'])
            db.session.add(permission_res)
        db.session.commit()

        print('权限数据预置数据插入完成！')


    if not User.query.first():

        User_data = [
            {'id':1,'name':'超级管理员','username':'admin','password':'SXygsj123,./','user_status':'1'},
            {'id':2,'name':'管理员','username':'admin123','password':'SXygsj123','user_status':'1'},
        ]
        for i in User_data:
            User_res = User(id=i['id'],username=i['username'],password=password_encryption(i['password']),name=i['name'],user_status=i['user_status'])
            db.session.add(User_res)
        db.session.commit()

        print('用户预置数据插入完成！')


    if not UserPermission.query.first():

        UserPermission_data = [
            {'id':1,'user_id':1,'permission_id':1,},
            {'id':2,'user_id':2,'permission_id':2,},
        ]

        for i in UserPermission_data:
            user_permission_res = UserPermission(id=i['id'],user_id=i['user_id'],permission_id=i['permission_id'])
            db.session.add(user_permission_res)
        db.session.commit()

        print('用户权限预置数据插入完成！')

    if not PermissionMenu.query.first():

        Permission_menu_data = [
            {'id':1,'premission_id':1,'menu_id':1,'permissions_list':'[c,r,u,d]'},
            {'id':2,'premission_id':1,'menu_id':2,'permissions_list':'[c,r,u,d]'},
            {'id':3,'premission_id':1,'menu_id':3,'permissions_list':'[c,r,u,d]'},
            {'id':4,'premission_id':1,'menu_id':4,'permissions_list':'[c,r,u,d]'},
            {'id':5,'premission_id':1,'menu_id':5,'permissions_list':'[c,r,u,d]'},
            {'id':6,'premission_id':1,'menu_id':6,'permissions_list':'[c,r,u,d]'},
            {'id':7,'premission_id':1,'menu_id':7,'permissions_list':'[c,r,u,d]'},
            {'id':8,'premission_id':1,'menu_id':8,'permissions_list':'[c,r,u,d]'},
            {'id':9,'premission_id':1,'menu_id':9,'permissions_list':'[c,r,u,d]'},
            {'id':10,'premission_id':1,'menu_id':10,'permissions_list':'[c,r,u,d]'},
            {'id':11,'premission_id':1,'menu_id':11,'permissions_list':'[c,r,u,d]'},
            {'id':12,'premission_id':1,'menu_id':12,'permissions_list':'[c,r,u,d]'},
            {'id':13,'premission_id':1,'menu_id':13,'permissions_list':'[c,r,u,d]'},
            {'id': 14, 'premission_id': 1, 'menu_id': 14, 'permissions_list': '[c,r,u,d]'},
            {'id': 15, 'premission_id': 1, 'menu_id': 15, 'permissions_list': '[c,r,u,d]'},
            {'id': 16, 'premission_id': 1, 'menu_id': 16, 'permissions_list': '[c,r,u,d]'},
            {'id': 17, 'premission_id': 1, 'menu_id': 17, 'permissions_list': '[c,r,u,d]'},
            {'id': 18, 'premission_id': 1, 'menu_id': 18, 'permissions_list': '[c,r,u,d]'},
            {'id': 19, 'premission_id': 1, 'menu_id': 19, 'permissions_list': '[c,r,u,d]'},
            {'id': 20, 'premission_id': 1, 'menu_id': 20, 'permissions_list': '[c,r,u,d]'},

            {'id': 21, 'premission_id': 2, 'menu_id': 1, 'permissions_list': '[c,r,u,d]'},
            {'id': 22, 'premission_id': 2, 'menu_id': 2, 'permissions_list': '[c,r,u,d]'},
            {'id': 23, 'premission_id': 2, 'menu_id': 3, 'permissions_list': '[c,r,u,d]'},
            {'id': 24, 'premission_id': 2, 'menu_id': 4, 'permissions_list': '[c,r,u,d]'},
            {'id': 25, 'premission_id': 2, 'menu_id': 5, 'permissions_list': '[c,r,u,d]'},
            {'id': 26, 'premission_id': 2, 'menu_id': 6, 'permissions_list': '[c,r,u,d]'},
            {'id': 27, 'premission_id': 2, 'menu_id': 7, 'permissions_list': '[c,r,u,d]'},
            {'id': 28, 'premission_id': 2, 'menu_id': 8, 'permissions_list': '[c,r,u,d]'},
            {'id': 29, 'premission_id': 2, 'menu_id': 9, 'permissions_list': '[c,r,u,d]'},
            {'id': 30, 'premission_id': 2, 'menu_id': 10, 'permissions_list': '[c,r,u,d]'},
            {'id': 31, 'premission_id': 2, 'menu_id': 11, 'permissions_list': '[c,r,u,d]'},
            {'id': 32, 'premission_id': 2, 'menu_id': 12, 'permissions_list': '[c,r,u,d]'},
            {'id': 33, 'premission_id': 2, 'menu_id': 13, 'permissions_list': '[c,r,u,d]'},
            {'id': 34, 'premission_id': 2, 'menu_id': 14, 'permissions_list': '[c,r,u,d]'},
            {'id': 35, 'premission_id': 2, 'menu_id': 15, 'permissions_list': '[c,r,u,d]'},
            {'id': 36, 'premission_id': 2, 'menu_id': 16, 'permissions_list': '[c,r,u,d]'},
            {'id': 37, 'premission_id': 2, 'menu_id': 17, 'permissions_list': '[c,r,u,d]'},
            {'id': 39, 'premission_id': 2, 'menu_id': 18, 'permissions_list': '[c,r,u,d]'},
            {'id': 40, 'premission_id': 2, 'menu_id': 20, 'permissions_list': '[c,r,u,d]'},
        ]

        for i in Permission_menu_data:
            permission_menu_res = PermissionMenu(id=i['id'],premission_id=i['premission_id'],menu_id=i['menu_id'],permissions_list=i['permissions_list'])
            db.session.add(permission_menu_res)
        db.session.commit()

        print('权限菜单预置数据插入完成！')


    if not Dict_data.query.first():

        # dict_type = [
        #     {'id':1,'type_name':'矿名编码表','type_code':'1','enable':'1'},
        #     {'id': 2, 'type_name': '算法类型编码表', 'type_code': '2', 'enable': '1'},
        #     {'id': 3, 'type_name': '厂商编码表', 'type_code': '3', 'enable': '1'}
        # ]
        dict_type = [
            {'type_name':'矿名编码表','type_code':'1','enable':'1'},
            { 'type_name': '算法类型编码表', 'type_code': '2', 'enable': '1'},
            {'type_name': '厂商编码表', 'type_code': '3', 'enable': '1'}
        ]
        for i in dict_type:
            # type = DictType(id=i['id'],type_name=i['type_name'],type_code=i['type_code'],enable=i['enable'])
            type = DictType(type_name=i['type_name'], type_code=i['type_code'], enable=i['enable'])
            db.session.add(type)
            db.session.commit()

        dict_data = [
            {'id':1,'data_id':0,'data_label':'北峪','data_value':'beiyu','type_code':'1', 'enable':'1'},
            {'id': 2,'data_id':0, 'data_label': '富家凹', 'data_value': 'fujiawa', 'type_code': '1', 'enable': '1'},
            {'id': 3,'data_id':0, 'data_label': '凤凰台', 'data_value': 'fenghuangtai', 'type_code': '1', 'enable': '1'},
            {'id': 4, 'data_id':0,'data_label': '官庄河', 'data_value': 'guzhuanghe', 'type_code': '1', 'enable': '1'},
            {'id': 5, 'data_id':0,'data_label': '集广', 'data_value':'jiguang', 'type_code': '1', 'enable': '1'},
            {'id': 6, 'data_id':0, 'data_label': '气动式钻机检测算法模型', 'data_value': 'pneumatic_machine', 'type_code': '2', 'enable': '1'},
            {'id': 7, 'data_id':0,'data_label': '液压式钻机检测算法模型', 'data_value': 'hydraulic_machine', 'type_code': '2',
             'enable': '1'},
            {'id': 8, 'data_id':0,'data_label': '履带式钻机检测算法模型', 'data_value': 'crawler_machine', 'type_code': '2',
             'enable': '1'},
            {'id': 9, 'data_id':0,'data_label': '未戴安全帽算法模型', 'data_value': 'helmet', 'type_code': '2',
             'enable': '1'},
            {'id': 10, 'data_id':0, 'data_label': '危险区域入侵算法模型', 'data_value': '', 'type_code': '2',
             'enable': '1'},
            {'id': 11, 'data_id':1,'data_label': '寒武纪', 'data_value': 'Cambricon', 'type_code': '3',
             'enable': '1'},
            {'id': 12, 'data_id':2,'data_label': '算能', 'data_value': 'Sophon', 'type_code': '3',
             'enable': '1'},
            {'id': 13,  'data_id':3,'data_label': '华为', 'data_value': 'Huawei', 'type_code': '3',
             'enable': '1'},
        ]

        for i in dict_data:
            dict_res = Dict_data(id=i['id'],data_id=i['data_id'] if i.get('data_id') else None,data_label=i['data_label'],data_value=i['data_value'],type_code=i['type_code'],enable=i['enable'])
            db.session.add(dict_res)
        db.session.commit()

        print('字典翻译数据插入完成！')

    if not Mine.query.first():

        mine_data = [
            {'id':1,'mine_name':'煤业'},
        ]

        for i in mine_data:
            mine_res = Mine(id=i['id'],mine_name=i['mine_name'])
            db.session.add(mine_res)
        db.session.commit()
        print('矿数据插入完成！')

    if not Diagnosis_type.query.first():

        diag_data = [
            {'id':1,'name':'视频抖动','prewarn':30,'warn':70},
            {'id':2,'name':'条纹干扰','prewarn':30,'warn':70},
            {'id':3,'name':'视频丢失','prewarn':30,'warn':70},
            {'id':4,'name':'视频遮挡','prewarn':30,'warn':70},
            {'id':5,'name':'视频冻结','prewarn':30,'warn':70},
            {'id':6,'name':'高亮度','prewarn':30,'warn':70},
            {'id':7,'name':'低亮度','prewarn':30,'warn':70},
            {'id':8,'name':'视频噪声','prewarn':30,'warn':70},
            {'id':9,'name':'偏色','prewarn':30,'warn':70},
            {'id':10,'name':'清晰度','prewarn':30,'warn':70},
            {'id':11,'name':'场景变化','prewarn':30,'warn':70},
            {'id':12,'name':'对比度','prewarn':30,'warn':70},
            {'id':13,'name':'横纹干扰','prewarn':30,'warn':70},
            {'id':14,'name':'滚动条纹','prewarn':30,'warn':70},
            {'id':15,'name':'横波干扰','prewarn':30,'warn':70},
            {'id': 16, 'name': '花帧检测', 'prewarn': 90, 'warn': 99},
        ]

        for i in diag_data:
            diag_res = Diagnosis_type(id=i['id'],name=i['name'],prewarn=i['prewarn'],warn=i['warn'])
            db.session.add(diag_res)
        db.session.commit()
        print('诊断类型检测项表！')

